//-----------------------------------------------------------------------------
// Copyright (c) 2011 David Waby
// Initially created on 11/11/2011
//-----------------------------------------------------------------------------

#include "Global.h"
#include "ItemStore.h"
#include "Item.h"
#include "Container.h"
#include "InventoryItem.h"
#include "InventoryContainer.h"

//-----------------------------------------------------------------------------
ItemStore::ItemStore()
{
} 
	
//-----------------------------------------------------------------------------
ItemStore::~ItemStore()
{
	for (int itemCount = 0; itemCount < m_noofAllItems; ++itemCount)
	{
		delete m_allItems[itemCount];
	}
	delete m_allItems;
} 

//-----------------------------------------------------------------------------
InventoryItem * ItemStore::CreateDefaultInventoryItemFromTemplate(int const i_itemID) const
{
	InventoryItem * newItem = NULL;

	switch (m_allItems[i_itemID]->GetItemType())
	{
		default:
		case ItemType::Miscellaneous:
		{
			newItem = new InventoryItem(m_allItems[i_itemID]);
		}
		case ItemType::Container:
		{
			newItem = new InventoryContainer(m_allItems[i_itemID]);
		}
	};
	 
	return newItem;
}

//-----------------------------------------------------------------------------
void ItemStore::Serialise(Json::Value * o_root) const
{
}

//-----------------------------------------------------------------------------
void ItemStore::Deserialise(Json::Value const * i_root)
{
	// Get the number of items and create array to hold them
	m_noofAllItems = i_root->size();
	m_allItems = new Item*[m_noofAllItems];

	// Loop through all of the items and add to the list
	Json::Value const * currentItem = NULL;
	for (int itemCount = 0; itemCount < m_noofAllItems; ++itemCount)
	{
		currentItem = &((*i_root)[itemCount]);
		int const index = (*currentItem)["id"].asInt();

		ItemType::Enum itemType = static_cast<ItemType::Enum>(currentItem->get("itemType", 0).asInt());
		switch (itemType)
		{
			default:
			case ItemType::Miscellaneous:
			{
				m_allItems[index] = new Item();
			}
			case ItemType::Container:
			{
				m_allItems[index] = new Container();
			}
		};

		m_allItems[index]->Deserialise(currentItem);
	}
}
